<?php
include_once("../configure/admin.config.inc.php");	//--> admin global var
include_once("db.inc.php");							//--> db global var
include_once("Smarty.class.php");					//--> out template
include_once("Operation.php");						//--> Operation
include_once("Customer.php");						//--> Customer
include_once("Country.php");						//--> Country
include_once("AddressBook.php");					//--> AddressBook
include_once("Validation.php");						//--> Validation

require_once("controlHeader.php");					//--> system control header

$objOperate 	= new Operation($objSession->getLanguage());			//--> Operation
$objCustomer	= new Customer(DB_TAG_SYSTEM, $uid);					//--> Customer
$objCountry		= new Country($uid);									//--> Country
$objAddressBook	= new AddressBook($uid);								//--> AddressBook
$objValidation	= new Validation($uid);									//--> Validation

$error_message 	= '';

$gender			= '';
$firstname		= '';
$lastname		= '';
$email			= '';
$oldemail		= '';

$telephone		= '';
$fax			= '';

$zoneid			= 1;
$countryid		= 223;
$company		= '';
$postcode		= '';

$state			= '';
$city			= '';
$street			= '';
		
$newsletter		= 'unsubscribed';
$address_id		= 0;
$password		= '';

$arrAddressBook	= array();

$arrOperate = $objOperate->arrGetFromGroupIDAndMenuID($gid, $menuid);
if(!empty($id)){
	$arrData 		= $objCustomer->getFromID($arrOperate, $id);
	$firstname 		= $arrData['firstname'];
 	$lastname		= $arrData['lastname'];
 	$oldemail		= $arrData['email'];
	$email 			= $arrData['email'];

	$address_id		= $arrData['default_address_id'];
	$telephone		= $arrData['telephone'];
	$fax			= $arrData['fax'];
	$newsletter		= $arrData['newsletter'];
	$password		= $arrData['password'];

	if($address_id > 0){
		$arrAddressBook = $objAddressBook->getByID($arrOperate, $address_id);
		
		$countryid	= $arrAddressBook['countryid'];
		$company	= $arrAddressBook['company'];
		$postcode	= $arrAddressBook['postcode'];
		
		$state		= $arrAddressBook['state'];
		$city		= $arrAddressBook['city'];
		$street		= $arrAddressBook['street'];
		
	}
		
}

if(isset($_POST['Submit'])){
	$id 			= $_POST['id'];
	$firstname 		= $_POST['firstname'];
	$lastname 		= $_POST['lastname'];
	$email 			= $_POST['email'];
	
	$telephone		= $_POST['telephone'];
	$fax			= $_POST['fax'];
		
	$countryid		= $_POST['countryid'];
	$company		= $_POST['company'];
	$postcode		= $_POST['postcode'];
	
	$state			= $_POST['state'];
	$city			= $_POST['city'];
	$street			= $_POST['street'];
			
	$newsletter		= $_POST['newsletter'];


	if(empty($firstname))
		$error_message = 'The first name should\'t be empty.';
	else if(empty($lastname)){
		$error_message = 'The first name should\'t be empty.';
	}else if(empty($email)){
		$error_message = 'The email should\'t be empty.';
	}else if(!$objValidation->isEmail($email)){
		$error_message = 'The email format invalid.';
	}else if($objCustomer->IsExistEmail($email) && $email != $oldemail){
		$error_message = 'the email be existed.';
	}
	
	if(empty($error_message)){
		if($address_id > 0)
			$objAddressBook->edit($address_id, $id, $gender, $company, $firstname, $lastname, $street, $suburb, $postcode, $city, $state, $countryid, $zoneid);
		
		$isReturnOrg = $objCustomer->edit($arrOperate, $id, $gender, $firstname, $lastname, $email, $address_id, $telephone, $fax, $newsletter, $password);
		if($isReturnOrg)
			$error_message = 'edit successfully.';
		else{
			$error_message = 'edit failure.';
		}
	}
}

$arrCountries = $objCountry->getGroupList();

$arrNewsletter = array('subscribed' => 'Subscribed', 'unsubscribed' => 'Unsubscribed');


/*----- out html -----*/
$smarty = new Smarty();		//----- out template
$smarty->template_dir = TEMPLATE_SYS_DIR;
$smarty->compile_dir  = COMPILE_SYS_DIR;
$smarty->assign('menuGid', $menuGid);
$smarty->assign('menuid', $menuid);
$smarty->assign('id', $id);

$smarty->assign('error_message', $error_message);
$smarty->assign('backurl',$backurl);


$smarty->assign('firstname', $firstname);
$smarty->assign('lastname', $lastname);
$smarty->assign('email', $email);

$smarty->assign('countryid', $countryid);
$smarty->assign('arrCountries', $arrCountries);

$smarty->assign('newsletter', $newsletter);
$smarty->assign('arrNewsletter', $arrNewsletter);

$smarty->assign('telephone', $telephone);
$smarty->assign('fax', $fax);
$smarty->assign('company', $company);
$smarty->assign('postcode', $postcode);
$smarty->assign('state', $state);
$smarty->assign('city', $city);
$smarty->assign('street', $street);


$smarty->display('listCustomer_Edit.htm');
?>
